package zjut.raymond.collectorplus.vm;

import android.os.Handler;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import zjut.raymond.collectorplus.pojo.Area;
import zjut.raymond.collectorplus.pojo.ReferencePoint;

public class MainViewModel extends ViewModel {
    /**
     * 单位相关信息
     */
    private MutableLiveData<List<Area>> businessMutableList;
    private List<Area> businesses = new ArrayList<>();

    public MutableLiveData<List<Area>> getBusinessMutableList() {
        if (businessMutableList == null) {
            businessMutableList = new MutableLiveData<>();
            loadBusinessList();
        }
        return businessMutableList;
    }

    private void loadBusinessList() {
        Handler handler = new Handler();
        handler.postDelayed(() -> getBusinessMutableList().postValue(businesses), 1000);
    }

    public void addBusiness(Area b) {
        if (!businesses.contains(b)) {
            businesses.add(b);
            getBusinessMutableList().postValue(businesses);
        }
    }

    public List<Area> getBusinesses() {
        return businesses;
    }

    public int getBusinessNumber() {
        return businesses.size();
    }


    /**
     * 建筑物相关信息
     */
    private MutableLiveData<List<Area>> buildingMutableList;
    private List<Area> buildings = new ArrayList<>();

    public MutableLiveData<List<Area>> getBuildingMutableList() {
        if (buildingMutableList == null) {
            buildingMutableList = new MutableLiveData<>();
            loadBuildingList();
        }
        return buildingMutableList;
    }

    private void loadBuildingList() {
        Handler handler = new Handler();
        handler.postDelayed(() -> getBuildingMutableList().postValue(buildings), 1000);
    }

    public void addBuilding(Area building) {
        if (!buildings.contains(building)) {
            buildings.add(building);
            getBuildingMutableList().postValue(buildings);
        }
    }

    public List<Area> getBuildings() {
        return buildings;
    }

    public int getBuildingsNumber() {
        return buildings.size();
    }

    public void clearBuildings() {
        buildings.clear();
        getBuildingMutableList().setValue(buildings);
    }

    /**
     * 楼层相关信息
     */
    private MutableLiveData<List<Area>> floorMutableList;
    private List<Area> floors = new ArrayList<>();

    public MutableLiveData<List<Area>> getFloorMutableList() {
        if (floorMutableList == null) {
            floorMutableList = new MutableLiveData<>();
            loadFloorList();
        }
        return floorMutableList;
    }

    private void loadFloorList() {
        Handler handler = new Handler();
        handler.postDelayed(() -> getFloorMutableList().postValue(floors), 1000);
    }

    public void addFloor(Area floor) {
        if (!floors.contains(floor)) {
            floors.add(floor);
            getFloorMutableList().postValue(floors);
        }
    }

    public List<Area> getFloors() {
        return floors;
    }

    public int getFloorsNumber() {
        return floors.size();
    }

    public void clearFloors() {
        floors.clear();
        getBuildingMutableList().setValue(floors);
    }

    /**
     * 当前已选择的business
     */
    private MutableLiveData<Area> currentBusiness;

    public MutableLiveData<Area> getCurrentBusiness() {
        if (currentBusiness == null) {
            currentBusiness = new MutableLiveData<>();
            currentBusiness.setValue(new Area("", "未选择"));
        }
        return currentBusiness;
    }

    public void setCurrentBusiness(Area area) {
        Area business = getCurrentBusiness().getValue();
        business.setId(area.getId());
        business.setName(area.getName());
        getCurrentBusiness().postValue(business);
    }

    /**
     * 当前已选择的building
     */
    private MutableLiveData<Area> currentBuilding;

    public MutableLiveData<Area> getCurrentBuilding() {
        if (currentBuilding == null) {
            currentBuilding = new MutableLiveData<>();
            currentBuilding.setValue(new Area("", "未选择"));
        }
        return currentBuilding;
    }

    public void setCurrentBuilding(Area area) {
        Area building = getCurrentBuilding().getValue();
        building.setId(area.getId());
        building.setName(area.getName());
        getCurrentBuilding().postValue(building);
    }

    public void clearCurrentBuilding() {
        setCurrentBuilding(new Area("", "未选择"));
    }

    /**
     * 当前已选择的floor
     */
    private MutableLiveData<Area> currentFloor;

    public MutableLiveData<Area> getCurrentFloor() {
        if (currentFloor == null) {
            currentFloor = new MutableLiveData<>();
            currentFloor.setValue(new Area("", "未选择"));
        }
        return currentFloor;
    }

    public void setCurrentFloor(Area area) {
        Area floor = getCurrentFloor().getValue();
        floor.setId(area.getId());
        floor.setName(area.getName());
        getCurrentFloor().postValue(floor);
    }

    public void clearCurrentFloor() {
        setCurrentFloor(new Area("", "未选择"));
    }

    public boolean areaSelected() {
        Area business = getCurrentBusiness().getValue();
        Area building = getCurrentBuilding().getValue();
        Area floor = getCurrentFloor().getValue();
        if(business != null && building != null && floor != null) {
            String businessId = business.getId();
            String buildingId = building.getId();
            String floorId = floor.getId();
            return (!businessId.equals("") && !buildingId.equals("") && !floorId.equals(""));
        }
        return false;
    }

    public String getAreaInfo() {
        if(areaSelected()) {
            String businessName = Objects.requireNonNull(getCurrentBusiness().getValue()).getName();
            String buildingName = Objects.requireNonNull(getCurrentBuilding().getValue()).getName();
            String floorName = Objects.requireNonNull(getCurrentFloor().getValue()).getName();
            return businessName + " - " + buildingName + " - " + floorName;
        }
        return "单位 - 建筑 - 楼层";
    }

    /**
     * 当前的参考点列表
     */

    public MutableLiveData<List<ReferencePoint>> referencePointLiveData;
    private List<ReferencePoint> referencePointList = new ArrayList<>();

    public MutableLiveData<List<ReferencePoint>> getReferencePointLiveData() {
        if (referencePointLiveData == null) {
            referencePointLiveData = new MutableLiveData<>();
            loadReferencePointList();
        }
        return referencePointLiveData;
    }

    private void loadReferencePointList() {
        Handler handler = new Handler();
        handler.postDelayed(() -> getReferencePointLiveData().postValue(referencePointList), 1000);
    }

    public void addReferencePoint(ReferencePoint referencePoint) {
        if (!referencePointList.contains(referencePoint)) {
            referencePointList.add(referencePoint);
            getReferencePointLiveData().postValue(referencePointList);
        }
    }

    public int getRpSize() {
        return referencePointList.size();
    }

    public void clearReferencePointList() {
        referencePointList.clear();
        getReferencePointLiveData().postValue(referencePointList);
    }

    /**
     * 扫描次数
     */
    private MutableLiveData<Integer> scanCount;

    public MutableLiveData<Integer> getScanCount() {
        if (scanCount == null) {
            scanCount = new MutableLiveData<>();
            scanCount.setValue(0);
        }
        return scanCount;
    }

    public void addScanCount() {
        getScanCount().setValue(getScanCount().getValue() + 1);
    }

    public void clearScanCount() {
        getScanCount().setValue(0);
    }
}